package com.yeshen.pmtea.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yeshen.pmtea.R;
import com.yeshen.pmtea.beans.News;
import com.yeshen.pmtea.callback.ImagecallBack;
import com.yeshen.pmtea.chache.MyLruCache;
import com.yeshen.pmtea.net.ImageAsyncTask;
import com.yeshen.pmtea.utils.SdCardUtils;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2016/11/15.
 */
public class NewsAdapter extends BaseAdapter {
    private Context mContext;
    private List<News> dataList;
    private News mNews;
    private MyLruCache mMyLruCache;
    public NewsAdapter(List<News> newsList, Context context) {
        this.mContext=context;
        this.dataList=newsList;
        int maxSize = (int) (Runtime.getRuntime().maxMemory()/8);
        mMyLruCache=new MyLruCache(maxSize);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret =null;
        ViewHolder holder=null;
        if (convertView!=null){
            ret =convertView;
            holder= (ViewHolder) ret.getTag();
        }else {
            ret= LayoutInflater.from(mContext).inflate(R.layout.news_item,parent,false);
            holder=new ViewHolder();
            holder.mImageView = (ImageView) ret.findViewById(R.id.img);
            holder.title= (TextView) ret.findViewById(R.id.title);
            holder.source = (TextView) ret.findViewById(R.id.source);
            holder.create_time = (TextView) ret.findViewById(R.id.create_time);
            holder.nickname = (TextView) ret.findViewById(R.id.nickname);
            //查找控件的数据作为Tag绑定给ret
            ret.setTag(holder);
        }
        //赋值
        //有上级的话是get(0).getWho().get(position)
         mNews =dataList.get(position);
        holder.title.setText(mNews.getTitle());
        holder.source.setText(mNews.getSource());
        holder.create_time.setText(mNews.getCreate_time());
        holder.nickname.setText(mNews.getNickname());

        //缓存是LruCache:latest最少,recently最近,used最近最少使用原则
        //LruChache内存缓存
        //从缓存获取数据
        Bitmap cacheBitmap=getCache(mNews.getWap_thumb());

        holder.mImageView.setTag(mNews.getWap_thumb());
        //Log.d("flag", "-----------getView: "+mNews.getWap_thumb());
        getNetImage(mNews.getWap_thumb(),holder.mImageView);
        return ret;
    }

    private Bitmap getCache(String wap_thumb) { 
        //从内存中获取数据--->myLruCache-->第一级
        wap_thumb=wap_thumb.replaceAll("/","");
        Bitmap bitmap = mMyLruCache.get(wap_thumb);
        if (bitmap!=null){
            Log.d("flag", "-----------getCache: 从内存获取数据");
           return bitmap;
        }else {//内存没有
            //就从磁盘获取数据
            String root =mContext.getExternalCacheDir().getAbsolutePath();
            String fileName =root+ File.separator+wap_thumb;
            byte[] bytes = SdCardUtils.getbyteFromFile(fileName);

            if (bytes!=null){
                Bitmap bitmapSD = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                //存到LrcCache中
                mMyLruCache.put(wap_thumb,bitmapSD);
                return bitmapSD;
            }
        }
        return null;
    }

    private void getNetImage(final String id, final ImageView imageView) {
        new ImageAsyncTask(new ImagecallBack() {
            @Override
            public void callBackImage(Bitmap bitmap) {
                //防止图片错位
                 String tag = (String) imageView.getTag();
               // Log.d("flag", "-----------callBackImage: "+tag);
                if (tag.equals(id)){

                    imageView.setImageBitmap(bitmap);
                }else {
                    imageView.setImageResource(R.mipmap.ic_launcher);
                }
            }
        }).execute(id);
    }

    private static class ViewHolder{
        private ImageView mImageView;
        private TextView title,source,create_time,nickname;
    }
}
